<?php
$date = date('D, d M Y H:i:s').' GMT';

$app_id = "5f98fe98";
$apiKey = "8b743b9b529e97a96e4483e9d010b66f";
$apiSecret = "4157e4fb79029822eeb931c037a53523";
$par = [
	"authorization" => '',
	"date" => $date,
	"host" => 'tts-api.xfyun.cn',
];
$signature = base64_encode(hash_hmac('sha256', "host: {$par['host']}\ndate: {$par['date']}\nGET /v2/tts HTTP/1.1", $apiSecret, true));
//var_dump(hash_hmac('sha256', "host: {$par['host']}\ndate: {$par['date']}\nGET /v2/tts HTTP/1.1", $apiSecret, true));
//var_dump("host: {$par['host']}\ndate: {$par['date']}\nGET /v2/tts HTTP/1.1");
//var_dump($signature);
$par["authorization"] = base64_encode("api_key=\"{$apiKey}\", algorithm=\"hmac-sha256\", headers=\"host date request-line\", signature=\"{$signature}\"");
//var_dump($par);

include "websocket_client.php";
$errstr = "";
$ws = websocket_open("tts-api.xfyun.cn", 80, '', $errstr, 10, false, false, "/v2/tts?".http_build_query($par));
if($errstr){
	echo $errstr;
	return;
}
$sd = [
	"common" => [
		"app_id" => $app_id
	],	
	"business" => [
		"aue" => "raw",
		"vcn" => 'xiaoyan',
		"tte" => "UNICODE"
	],
	"data" => [
		"status" => 2,
		"text" => base64_encode(iconv('UTF-8', 'UCS-2LE', "你好"))//base64_encode("ياخشىمۇسىز")//
	],

];
//var_dump($sd);
websocket_write($ws, json_encode($sd));
$sd = [
	"data" => [
		"status" => 2
	]
];
websocket_write($ws, json_encode($sd));
$rd = websocket_read($ws, $errstr);
var_dump($rd);
var_dump($errstr);
